Javascript Looping examples

if ..else..

if (a < 1) {
skill = "Java";
} else if (a < 2) {
skill = "PHP";
} else {
skill = "Servicenow";
}

...................................................................
For loop

Example 1:

for (i = 1; i < 5; i++) {
text += "Servicenow chapter " + i + "
";
}

Example 2:
j=1;
for (; j < 5; j++) {
text += "Servicenow chapter " + i + "
";
}

Example 3:

var trainer = {name:"Pavan", skill:"Java", age:28};

var text = "";
var x;
for (x in trainer) {
text += trainer[x];
}

Example 4:
var txt = "";
var trainer = [{name:"Pavan", skill:"Java", age:28},{name:"Paavan", skill:"Java", age:28}];

var x;
for (x in trainer) {
txt += trainer[x]["name"];
}

document.getElementById("demo").innerHTML = txt;

..........................................................................................
switch

switch (new Date().getDay()) {
case 0:
day = "Tody is Sunday";
break;
case 1:
day = "Tody is Tody is Monday";
break;
case 2:
day = "Tody is Tuesday";
break;
case 3:
day = "Tody is Wednesday";
break;
case 4:
day = "Tody is Thursday";
break;
case 5:
day = "Tody is Friday";
break;
case 6:
day = "Tody is Saturday";
break;
default:
day = "No Default";
}

-----------------------------------------------------------------------------------------
while loop


while (i < 10) {
text += "The number is " + i;
i++;
}

do while loop

do {
text += "The number is " + i;
i++;
}
while (i < 10);

.....................................................